package com.smartcruise.task.nodeTaskDataEdit;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.smartcruise.R;
import com.smartcruise.base.BaseActivity;
import com.smartcruise.data.cruiseData.CruiseContract;
import com.smartcruise.data.cruiseData.CruiseData;
import com.smartcruise.data.cruiseData.NodeTaskListBean;
import com.smartcruise.report.EditType;
import com.smartcruise.util.Key;
import com.smartcruise.util.ListUtils;
import com.smartcruise.util.TimeUtils;
import com.smartcruise.util.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

/**
 * Created by FengChaoQun
 * on 2018/1/25 0025
 * 任务节点数据编辑界面
 */

public class NodeTaskDataEditActivity extends BaseActivity implements NodeTaskDataEditContract.View {

    @BindView(R.id.leftArrow)
    ImageView leftArrow;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.rightText)
    TextView rightText;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.coordinate)
    TextView coordinate;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.content)
    EditText content;
    @BindView(R.id.camera)
    ImageView camera;
    @BindView(R.id.audio)
    ImageView audio;
    @BindView(R.id.video)
    ImageView video;
    @BindView(R.id.scan)
    ImageView scan;
    @BindView(R.id.audioGrid)
    GridView audioGrid;
    @BindView(R.id.imageGrid)
    GridView imageGrid;
    @BindView(R.id.videoList)
    ListView videoList;
    @BindView(R.id.delete)
    Button delete;
    @BindView(R.id.templeSave)
    Button templeSave;
    @BindView(R.id.upReport)
    Button upReport;
    @BindView(R.id.scanResultLay)
    LinearLayout scanResultLay;

    private NodeTaskDataEditContract.Presenter mPresenter;
    private EditType editType;
    private CruiseData cruiseData;
    private NodeTaskListBean nodeTaskListBean;
    private String nodeId;

    public static void start(Context context, String taskId, String nodeId) {
        Intent intent = new Intent(context, NodeTaskDataEditActivity.class);
        intent.putExtra("taskId", taskId);
        intent.putExtra("nodeId", nodeId);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_node_task_data_edit);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {

        String id = getIntent().getStringExtra("taskId");
        nodeId = getIntent().getStringExtra("nodeId");
        if (TextUtils.isEmpty(id) || TextUtils.isEmpty(nodeId)) {
            showToast(getString(R.string.needId));
            finish();
            return;
        }
        cruiseData = realm.where(CruiseData.class).equalTo(Key.id, id).findFirst();
        if (cruiseData == null) {
            showToast(getString(R.string.noContent));
            finish();
            return;
        }

        if (!ListUtils.isHaveContent(cruiseData.getNodeTaskList())) {
            showToast("该任务没有节点任务");
            finish();
            return;
        }

        nodeTaskListBean = cruiseData.getNodeTaskList().where().equalTo(Key.nodeId, nodeId).findFirst();
        if (nodeTaskListBean == null) {
            showToast("没有找到该节点任务");
            finish();
            return;
        }

        if (CruiseContract.isCanModify(cruiseData)) {
            editType = EditType.MODIFY;
        } else {
            editType = EditType.VIEW;
        }

        name.setText(nodeTaskListBean.getPatrollingNodeEntity().getName());
        String lat = nodeTaskListBean.getPatrollingNodeEntity().getCoordinateList().get(0).getLat().substring(0, 8);
        String lng = nodeTaskListBean.getPatrollingNodeEntity().getCoordinateList().get(0).getLng().substring(0, 8);
        coordinate.setText(lat + ":" + lng);
        time.setText(TimeUtils.getSimpleTimeString(TimeUtils.currentTime()));

        mPresenter = new NodeTaskDataEditPresenterImp(this, editType, cruiseData, nodeTaskListBean);
        mPresenter.start();

        if (!nodeTaskListBean.isNeedScan() && !nodeTaskListBean.isNeedText() && !nodeTaskListBean.isNeedPicture()
                && !nodeTaskListBean.isNeedVideo() && !nodeTaskListBean.isNeedAudio()) {
            finish();
            showToast("此巡护节点没有需要完成的任务");
            return;
        }

        if (editType == EditType.VIEW) {//预览模式不能编辑
            content.setFocusable(false);
            content.setFocusableInTouchMode(false);
            camera.setVisibility(View.GONE);
            video.setVisibility(View.GONE);
            audio.setVisibility(View.GONE);
            scan.setVisibility(View.GONE);
        } else {
            content.setVisibility(nodeTaskListBean.isNeedText() ? View.VISIBLE : View.GONE);
            camera.setVisibility(nodeTaskListBean.isNeedPicture() ? View.VISIBLE : View.GONE);
            video.setVisibility(nodeTaskListBean.isNeedVideo() ? View.VISIBLE : View.GONE);
            audio.setVisibility(nodeTaskListBean.isNeedAudio() ? View.VISIBLE : View.GONE);
            scan.setVisibility(nodeTaskListBean.isNeedScan() ? View.VISIBLE : View.GONE);
        }
    }

    @OnClick(R.id.leftArrow)
    public void onLeftArrowClicked() {
        mPresenter.clickOnBack();
    }

    @OnClick(R.id.rightText)
    public void onRightTextClicked() {
        showToast(getString(R.string.onDeveloping));
    }

    @OnClick(R.id.camera)
    public void onCameraClicked() {
        mPresenter.openCamera();
    }

    @OnClick(R.id.audio)
    public void onAudioClicked() {
        mPresenter.openAudioRecord();
    }

    @OnClick(R.id.video)
    public void onVideoClicked() {
        mPresenter.openVideo();
    }

    @OnClick(R.id.scan)
    public void onScanClicked() {
        mPresenter.openScan();
    }

    @OnClick(R.id.delete)
    public void onDeleteClicked() {
        mPresenter.delete();
    }

    @OnClick(R.id.templeSave)
    public void onTempleSaveClicked() {
        mPresenter.clickOnSave();
    }

    @OnClick(R.id.upReport)
    public void onUpReportClicked() {
        mPresenter.clickOnUpload();
    }

    @Override
    public void setData(BaseAdapter audio, BaseAdapter image, BaseAdapter video) {
        audioGrid.setAdapter(audio);
        notifyAudiosChange(audio);
        imageGrid.setAdapter(image);
        notifyImagesChange(image);
        videoList.setAdapter(video);
        notifyVideosChange(video);
    }

    @Override
    public void setText(String text) {
        content.setText(text);
    }

    @Override
    public String getText() {
        return content.getText().toString();
    }

    @Override
    public void notifyAudiosChange(BaseAdapter baseAdapter) {
        if (baseAdapter.isEmpty()) {
            audioGrid.setVisibility(View.GONE);
        } else {
            audioGrid.setVisibility(View.VISIBLE);
            if (baseAdapter.getCount() > 3) {
                ViewGroup.LayoutParams layoutParams = audioGrid.getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height = Utils.px(200);
                audioGrid.setLayoutParams(layoutParams);
            } else {
                ViewGroup.LayoutParams layoutParams = audioGrid.getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height = Utils.px(100);
                audioGrid.setLayoutParams(layoutParams);
            }
        }
        baseAdapter.notifyDataSetChanged();
    }

    @Override
    public void notifyImagesChange(BaseAdapter baseAdapter) {
        if (baseAdapter.isEmpty()) {
            imageGrid.setVisibility(View.GONE);
        } else {
            imageGrid.setVisibility(View.VISIBLE);
            if (baseAdapter.getCount() > 3) {
                ViewGroup.LayoutParams layoutParams = imageGrid.getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height = Utils.px(200);
                imageGrid.setLayoutParams(layoutParams);
            } else {
                ViewGroup.LayoutParams layoutParams = imageGrid.getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height = Utils.px(100);
                imageGrid.setLayoutParams(layoutParams);
            }
        }
        baseAdapter.notifyDataSetChanged();
    }

    @Override
    public void notifyVideosChange(BaseAdapter baseAdapter) {
        if (baseAdapter.isEmpty()) {
            videoList.setVisibility(View.GONE);
        } else {
            videoList.setVisibility(View.VISIBLE);
            if (baseAdapter.getCount() == 1) {
                ViewGroup.LayoutParams layoutParams = videoList.getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height = Utils.px(200);
                videoList.setLayoutParams(layoutParams);
            } else if (baseAdapter.getCount() == 2) {
                ViewGroup.LayoutParams layoutParams = videoList.getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height = Utils.px(400);
                videoList.setLayoutParams(layoutParams);
            } else if (baseAdapter.getCount() == 3) {
                ViewGroup.LayoutParams layoutParams = videoList.getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height = Utils.px(600);
                videoList.setLayoutParams(layoutParams);
            }
        }
        baseAdapter.notifyDataSetChanged();
    }

    @Override
    public void showScanResult(boolean show) {
        scanResultLay.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    @Override
    public void showDeleteButton(boolean show) {
        delete.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    @Override
    public void showSaveButton(boolean show) {
        templeSave.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    @Override
    public void showUploadButton(boolean show) {
        upReport.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mPresenter.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (JCVideoPlayer.backPress()) {
                return true;
            }
            mPresenter.clickOnBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
